//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// This class adds commands to the context menus of the nodes in the
	/// Query Application Block node hierarchy.
	/// </summary>
	public class QueryCommandRegistrar : CommandRegistrar
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="T:QueryCommandRegistrar"/> class.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public QueryCommandRegistrar (IServiceProvider serviceProvider) : base(serviceProvider)
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Register the commands.
		/// </summary>
		public override void Register ()
		{
			AddQuerySettingsCommand();
			AddDefaultCommands(typeof(QuerySettingsNode));

			AddFileCommandCommand();
			AddDefaultCommands(typeof(FileCommandNode));

			AddServiceCommandCommand();
			AddDefaultCommands(typeof(ServiceCommandNode));

			AddParameterSetCommand();
			AddCopyCommand(typeof(ParameterSetNode));
			AddDefaultCommands(typeof(ParameterSetNode));

			AddCustomParameterCommand();
			AddDefaultCommands(typeof(CustomParameterNode));

			AddNameValueParameterCommand();
			AddDefaultCommands(typeof(NameValueParameterNode));

			AddXmlParameterCommand();
			AddDefaultCommands(typeof(XmlParameterNode));

			AddCustomQueryCommand();
			AddDefaultCommands(typeof(CustomQueryNode));

			AddWcfServiceQueryCommand();
			AddDefaultCommands(typeof(WcfServiceQueryNode));

			AddXmlFileQueryCommand();
			AddDefaultCommands(typeof(XmlFileQueryNode));
		}
		#endregion

		#region Private Methods
		private void AddQuerySettingsCommand ()
		{
			ConfigurationUICommand command = ConfigurationUICommand.CreateSingleUICommand(ServiceProvider,
																																										InternalResources.QuerySettingsUICommandText,
																																										InternalResources.QuerySettingsUICommandLongText,
																																										new AddQuerySettingsNodeCommand(ServiceProvider),
																																										typeof(QuerySettingsNode));
			AddUICommand(command, typeof(ConfigurationApplicationNode));
		}

		private void AddFileCommandCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.FileCommandUICommandText,
																	InternalResources.FileCommandUICommandLongText,
																	typeof(FileCommandNode),
																	typeof(CommandCollectionNode));
		}

		private void AddServiceCommandCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.ServiceCommandUICommandText,
																	InternalResources.ServiceCommandUICommandLongText,
																	typeof(ServiceCommandNode),
																	typeof(CommandCollectionNode));
		}

		private void AddParameterSetCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.ParameterSetUICommandText,
																	InternalResources.ParameterSetUICommandLongText,
																	typeof(ParameterSetNode),
																	typeof(ParameterCollectionNode));
		}

		private void AddCustomParameterCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.CustomParameterUICommandText,
																	InternalResources.CustomParameterUICommandLongText,
																	typeof(CustomParameterNode),
																	typeof(ParameterSetNode));
		}

		private void AddNameValueParameterCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.NameValueParameterUICommandText,
																	InternalResources.NameValueParameterUICommandLongText,
																	typeof(NameValueParameterNode),
																	typeof(ParameterSetNode));
		}

		private void AddXmlParameterCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.XmlParameterUICommandText,
																	InternalResources.XmlParameterUICommandLongText,
																	typeof(XmlParameterNode),
																	typeof(ParameterSetNode));
		}

		private void AddCustomQueryCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.CustomQueryUICommandText,
																	InternalResources.CustomQueryUICommandLongText,
																	typeof(CustomQueryNode),
																	typeof(QueryCollectionNode));
		}

		private void AddWcfServiceQueryCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.WcfServiceQueryUICommandText,
																	InternalResources.WcfServiceQueryUICommandLongText,
																	typeof(WcfServiceQueryNode),
																	typeof(QueryCollectionNode));
		}

		private void AddXmlFileQueryCommand()
		{
			AddMultipleChildNodeCommand(InternalResources.XmlFileQueryUICommandText,
																	InternalResources.XmlFileQueryUICommandLongText,
																	typeof(XmlFileQueryNode),
																	typeof(QueryCollectionNode));
		}

		private void AddCopyCommand(Type queryNodeType)
		{
			ConfigurationUICommand command = new ConfigurationUICommand(ServiceProvider,
																																	InternalResources.CopyUICommandText,
																																	InternalResources.CopyUICommandLongText,
																																	CommandState.Enabled,
																																	new CopyParameterSetNodeCommand(ServiceProvider),
																																	Shortcut.None,
																																	InsertionPoint.Action,
																																	null);
			AddUICommand(command, queryNodeType);
			command.Dispose();
		}
		#endregion
	}
}